package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Lff
 * @date 2022/3/2
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
        implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public Page<SetmealDto> findAllByPage(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> pages = new Page<>(page, pageSize);
        setmealMapper.selecyMySetmealDishPage(pages, name);
        return pages;
    }

    @Override
    @Transactional
    public void addSetmealWithDish(SetmealDto setmealDto) {
        try {
            //1. 添加setmeal数据 获取setmeal的id
            Setmeal setmeal = new Setmeal();
            BeanUtils.copyProperties(setmealDto, setmeal);
            setmealMapper.insert(setmeal);
            //获取setmeal的id
            Long id = setmeal.getId();
            //2.根据setmeal的id来填充setmeal_dish中setmeal的id
            List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(id);
                //3.执行setmeal_dish的添加
                setmealDishMapper.insert(setmealDish);
            });
        } catch (BeansException e) {
            throw new CustomException("添加失败！");
        }
    }

    @Override
    public SetmealDto getOneById(Long id) {
        SetmealDto setmealDto = setmealMapper.getOneWithCategory(id);
        //获取setmealid
        LambdaQueryWrapper<SetmealDish> lqw = Wrappers.lambdaQuery(SetmealDish.class);
        lqw.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(lqw);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }


    @Override
    @Transactional
    public void updateSetmealWithDish(SetmealDto setmealDto) {
        //修改setmeal表
        try {
            LambdaQueryWrapper<Setmeal> setmealWrapper = Wrappers.lambdaQuery(Setmeal.class);
            //修改id为此setmeal的数据
            setmealWrapper.eq(Setmeal::getId, setmealDto.getId());
            Setmeal setmeal = new Setmeal();
            BeanUtils.copyProperties(setmealDto, setmeal);
            setmealMapper.updateById(setmeal);
            //修改setmealdish
            List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
            //只有dishid，setmealid没有填充
            setmealDishes.forEach(setmealDish -> {
                //填充setmealId
                setmealDish.setSetmealId(setmealDto.getId());
                LambdaQueryWrapper<SetmealDish> sdWrapper = Wrappers.lambdaQuery(SetmealDish.class);
                // 修改指定条件 setmealId 和 dishid
                sdWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
                sdWrapper.eq(SetmealDish::getDishId, setmealDish.getDishId());
                //执行修改
                //删除
                setmealDishMapper.delete(sdWrapper);
                //添加
                setmealDishMapper.insert(setmealDish);
            });
        } catch (BeansException e) {
            throw new CustomException("修改失败！");
        }
    }


    @Override
    @Transactional
    public void deleteByIds(Long[] ids) {
        //删除setmeal里的套餐信息
        LambdaQueryWrapper<Setmeal> setmealWrapper = Wrappers.lambdaQuery(Setmeal.class);
        setmealWrapper.in(Setmeal::getId, ids);
        setmealWrapper.eq(Setmeal::getStatus, 0);
        int delete = setmealMapper.delete(setmealWrapper);
        if (delete != ids.length) {
            throw new CustomException("所选套餐正在热卖，无法删除！");
        }
        //删除setmeal_dish中的数据
        LambdaQueryWrapper<SetmealDish> setmealDishWrapper = Wrappers.lambdaQuery(SetmealDish.class);
        setmealDishWrapper.in(SetmealDish::getSetmealId, ids);
        try {
            setmealDishMapper.delete(setmealDishWrapper);
        } catch (Exception e) {
            throw new CustomException("系统开小差了，请稍后再试..");
        }

    }


    @Override
    public void updateStatus(Integer status, Long[] ids) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        LambdaUpdateWrapper<Setmeal> setmealWrapper = Wrappers.lambdaUpdate(Setmeal.class);
        setmealWrapper.set(Setmeal::getStatus, setmeal.getStatus());
        setmealWrapper.in(Setmeal::getId, ids);
        //执行修改
        setmealMapper.update(setmeal, setmealWrapper);
    }

    @Override
    public List<Setmeal> getList(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> qw = Wrappers.lambdaQuery(Setmeal.class);
        qw.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
        qw.eq(Setmeal::getStatus, setmeal.getStatus());
        qw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealMapper.selectList(qw);
        return setmealList;
    }


}
